var i = 1;
var totalPage = "";
$(function(){
	var token=localStorage.getItem("token");
	$("#addTimeFrom").datetimepicker({lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d'
	});
	$("#addTimeTo").datetimepicker({lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d'
	});

	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'post',
		url :  server_url+'/rest/tea/order/getOrderListByUserId.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true,
		rowHoverColor:true
	});

	$("#commYesBtn").click(function(){subcancelReasonSave();});
	$("#commNoBtn").click(function(){subcancelReasonCancle();});
	$("#easyDialogNoBtn").click(function(){modifyStaffCancle();});
});
function operate(record, rowIndex, colIndex, options) {
	debugger
	//console.log(record.lstOrderDetail[0].supplyVo.supplyID);
	var aaa;
	if(record.status == "未发货"){
		aaa = '<a href="javascript:sendOrder(\'' + gridObj.getRecordIndexValue(record, 'id') + '\');">发货</a>';
	}else{
		aaa= '无';
	}
	return aaa + '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record, 'id') + '\');">订单详情</a>'
	
	
	if(record.orderStatus=='01'){
		return '<a href="javascript:modifyOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">修改</a>'+
	            '<a href="javascript:delOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">删除</a>'+
		  	    '<a href="javascript:findProduct(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">开始找货</a>'+
		        '<a href="javascript:cancleOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">取消订单</a>'+
			    '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">订单详情</a>';
	}else if(record.orderStatus=='02'||record.orderStatus=='03'){
		return '<a href="javascript:dealOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">处理</a>'+
		        '<a href="javascript:modifyOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">修改</a>'+
			    '<a href="javascript:delOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">删除</a>'+
			    '<a href="javascript:cancleOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">取消订单</a>'+
			    '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">订单详情</a>';
	}
	else if(record.orderStatus=='04'||record.orderStatus=='05'){
		return '<a href="javascript:dealOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">处理</a>'+
			    '<a href="javascript:modifyOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">修改</a>'+
			    '<a href="javascript:delOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">删除</a>'+
			    '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">订单详情</a>';
	}
	else if(record.orderStatus=='06'||record.orderStatus=='07') {
		return 	'<a href="javascript:modifyOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">修改</a>'+
			    '<a href="javascript:delOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">删除</a>'+
			    '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">订单详情</a>';
	}
	else{
		//return '<a href="javascript:viewOrder(\'' + gridObj.getRecordIndexValue(record.lstOrderDetail[0], 'orderID') + '\');">订单详情</a>';
	}
};
//function productName(record, rowIndex, colIndex, options){
//	alert(record);
//	return record.type;
//	
//};

// 数据表格参数查询
function doSearch() {
	var searchParames = $('#myform').serialize();
	debugger
	gridObj.search(searchParames);
}
function modifyOrder(id) {
	var url='orderManage/modifyOrder.do?id='+id;
	tl_config.loadToWorkArea(url);
}


function sendOrder(orderId){
	//alert(orderId);
	$('#overlay').css('display','block');
	$('#easyDialogBox').css('display','block');
	$('#easyDialogWrapper').css('display','block');
	$("#easyDialogYesBtn").click(function(){
		//alert(orderId);
		if("" == $("#staffModify").val() || $("#staffModify").val() == null){
			alert("请输入发送的内容");
			return;
		}
		$.ajax({
			type : 'POST',
			url : server_url+'/rest/tea/order/sendOrder.do?token='+token+'&ran='+Math.random(),
			data:{id:orderId,wayNo:$("#staffModify").val()},
			success : function(data) {
				if (data.retCode == "0000") {
					//$("#staff").val(staff);
					closeDialog ();
					alert("发送成功");
					tl_config.loadToWorkArea("orderManage/list.do");
					//reloadFn();
				}
			}
		});
	});
}






function delOrder(id) {
	if(empty(id)){
		easyDialog.open({
			container : {
				header : '温馨提示',
				content : '请选择要操作的记录！'
			},
			autoClose : 2000
		});
		return;
	}
	easyDialog.open({
		container : {
			header : '删除确认',
			content : '您确定要删除订单么？',
			yesFn : function(){
				$.ajax({
					url:server_url+'/rest/orderManage/delOrder.do?ran='+Math.random()+"&token="+token,
					type:'get',
					data:{orderId:id},
					success:function(data){
						if(data.retCode=="0000"){
							doSearch();
						}
						else{
							easyDialog.open({
								container : {
									header : '提示',
									content : data.retMsg
								},
								autoClose : 2000
							});
						}
					}
				})
			},
			noFn : true
		}
	});
}

//处理
function dealOrder(id) {
	var url='orderManage/dealOrder.do?id='+id;
	tl_config.loadToWorkArea(url);
}

//马上找货
function findProduct(id) {
	if(empty(id)){
		easyDialog.open({
			container : {
				header : '温馨提示',
				content : '请选择要操作的记录！'
			},
			autoClose : 2000
		});
		return;
	}
	easyDialog.open({
		container : {
			header : '开始找货确认',
			content : '您确定要开始找货么？',
			yesFn : function(){
				$.ajax({
					url:server_url+'/rest/orderManage/findOrder.do?ran='+Math.random()+"&token="+token,
					type:'get',
					data:{orderId:id},
					success:function(data){
						if(data.retCode=="0000"){
							doSearch();
						}
						else{
							easyDialog.open({
								container : {
									header : '提示',
									content : data.retMsg
								},
								autoClose : 2000
							});
						}
					}
				})
			},
			noFn : true
		}
	});
}
function cancleOrder(id) {
	subcancelReason(id);
}
function viewOrder(id){
	var url='orderManage/viewteaOrder.do?id='+id;
	tl_config.loadToWorkArea(url);
}

$(".tabTit li").click(function() {
	$(this).addClass('on').siblings().removeClass('on');
	var data;
	data = $(this).attr('data');
	$("#status").val(data);
	console.log(data);
	doSearch();

});

function productName(record, rowIndex, colIndex, options){
	
	var aaa = record.teaProduct;
	return record.teaProduct.productname;
	//return record.lstOrderDetail[0].productName;

};

function teaPurAd(record, rowIndex, colIndex, options){
	debugger
	var aaa = record.teaProduct;
	if(record.teaProduct.teaPurAd =="0"){
		return "现购";
	}
	if(record.teaProduct.teaPurAd =="1"){
		return "预购";
	}
	//return record.lstOrderDetail[0].productName;
	
};


function teaDeTime(record, rowIndex, colIndex, options){
	debugger
	var aaa = record.teaProduct;
	if(!"" == record.teaProduct.teaDeTime && record.teaProduct.teaDeTime != null ){
		return record.teaProduct.teaDeTime;
	}else{
		return "无";
	}
	
	//return record.lstOrderDetail[0].productName;
	
};
function supplyName(record, rowIndex, colIndex, options){
 
	var supply = record.supplyVo;
	//var supply = record.lstOrderDetail[0].supplyVo;
//	if(supply != null || supply != ''){
//		return supply.supplyName;
//	}else{
//		return "";
//	}

};
function usersname(record, rowIndex, colIndex, options){
	debugger
	var usersname = record.teaFrontusers.usersname;
	if(usersname != null || usersname != ''){
		return usersname;
	}else{
		return "";
	}
	//var supply = record.lstOrderDetail[0].supplyVo;
//	if(supply != null || supply != ''){
//		return supply.supplyName;
//	}else{
//		return "";
//	}
	
};
function supplyArea(record, rowIndex, colIndex, options){
	var supplyArea = record.supplyVo;
//	var supplyArea = record.lstOrderDetail[0].supplyVo;
//	if(supplyArea != null || supplyArea != ''){
//		return supplyArea.provinceName+','+supplyArea.cityName+','+supplyArea.countyName;
//	}else{
//		return "";
//	}
};
function buyerName(record, rowIndex, colIndex, options){
	//console.log(record.buyer);
	var buyerName = record.buyer;
//	if(buyerName != null || buyerName != ''){
//		return record.buyer.buyerName;
//	}else{
//		return "";
//	}
};
function buyerArea(record, rowIndex, colIndex, options){
	var buyerArea = record.buyer;
//	if(buyerArea != null || buyerArea != ''){
//		return buyerArea.provinceName+','+buyerArea.cityName+','+buyerArea.countyName;
//	}else{
//		return "";
//	}

}



function subcancelReason(id){
	$('#id').val(id);
	$('#overlayComm').css('display','block');
	$('#easyDialogBoxComm').css('display','block');
	$('#easyDialogWrapperComm').css('display','block');
}

function subcancelReasonSave(){
	var cancelReason= $("#cancelReason").val();
	$.ajax({
		type : 'GET',
		url : server_url+'/rest/orderManage/cancleOrder.do?token='+token+'&orderId='+$("#id").val()+'&cancelReason='+$("#cancelReason").val()+'&ran='+Math.random(),
		success : function(data) {
			if (data.retCode == "0000"){
				closesubcancelReasonDialog ();
			}
		}
	});
}

function subcancelReasonCancle(){
	closesubcancelReasonDialog ();
}


function closesubcancelReasonDialog (){
	$('#overlayComm').css('display','none');
	$('#easyDialogBoxComm').css('display','none');
	$('#easyDialogWrapperComm').css('display','none');
}

function modifyStaffCancle(){
	closeDialog ();
}

function closeDialog (){
	$('#overlay').css('display','none');
	$('#easyDialogBox').css('display','none');
	$('#easyDialogWrapper').css('display','none');
}

